package com.show.service.sys_menu;

import com.show.dao.sys_menu.MenuRepositoty;
import com.show.dto.sys_menu.SysMenu;
import com.show.dto.sys_role.SysRole;
import com.show.dto.sys_role.Ztree;
import com.show.util.PageUtil;
import com.show.util.QueryUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 顾志杰 on 2018/2/1.
 */
@Service
public class MenuService {
    @Autowired
    QueryUtil queryUtil;
    @Autowired
    MenuRepositoty menuRepositoty;
    /**
     * 查询菜单列表加分页
     *
     * @param cgmenu   菜单实体类
     * @param page     页码
     * @param pageSize 页容
     * @author 顾志杰
     * @date 2017/11/17 8:36
     */
    public PageUtil<SysMenu> findAll(SysMenu cgmenu, Integer page, Integer pageSize) {
        StringBuffer sql = new StringBuffer();
        if (null == page || "".equals(page) || "0".equals(page)) {//排空，设置默认值
            page = 1;
        }
        if (null == pageSize || "".equals(pageSize) || "0".equals(pageSize)) {//排空，设置默认值
            pageSize = 10;
        }
        sql.append("select * from sys_menu where sts='1'");
        if (StringUtils.isNotBlank(cgmenu.getMenuName())) {
            sql.append(" and menu_name like '%" + cgmenu.getMenuName() + "%'");//根据人员名称查询
        }
        PageUtil<SysMenu> roleMenus = queryUtil.queryByPage(sql.toString(), SysMenu.class, page, pageSize);

        return roleMenus;
    }

    /**
     * 删除菜单
     *
     * @param menuid 菜单id
     * @author 顾志杰
     * @date 2017/11/17 9:01
     */
    public Integer delete(Integer menuid) {
        SysMenu cgMenu = menuRepositoty.findOne(menuid);
        if (StringUtils.isBlank(cgMenu.getMenuUrl())) {
            return 0;

        } else {
            cgMenu.setSts(0);
            try {
                menuRepositoty.saveAndFlush(cgMenu);
                return 1;
            } catch (Exception e) {
                return 2;
            }
        }

    }


    /**
     * 根据设备id查询菜单
     *
     * @param mentid 菜单id
     * @author 顾志杰
     * @date 2017/11/13 16:10
     */
    public SysMenu findOne(Integer mentid) {
        return menuRepositoty.findOne(mentid);
    }

    /**
     * 修改菜单
     *
     * @param c 菜单实体类
     * @author 顾志杰
     * @date 2017/11/13 16:10
     */
    public String update(SysMenu c) {
        c.setSts(1);//可用状态
if(menuRepositoty.findOne(c.getMenuId()).getpMenuId()==null){
    return "一级菜单不能更改";
}else{
    try {
        menuRepositoty.saveAndFlush(c);
    } catch (Exception e) {
        return "修改失败";
    }
    return "修改成功";
}

    }

    /**
     * 查询菜单树
     * @author 顾志杰
     * @date 2018/2/1 11:01
     * @param
     */
    public List<Ztree> tree(){
        String sql="select menu_id id,menu_name name,p_menu_id pId from sys_menu where sts=1";
       return queryUtil.query(sql,Ztree.class);
    }

    /**
     * 查询菜单树
     * @author 顾志杰
     * @date 2018/2/1 11:01
     * @param
     */
    public List<Ztree> treeParent(){
        String sql="select menu_id id,menu_name name,p_menu_id pId from sys_menu where sts=1 and p_menu_id is null";
        return queryUtil.query(sql,Ztree.class);
    }

    /**
     * 查询父级菜单
     * @author baiyu
     * @date 2018/2/8 11:01
     * @param
     */
    public List<SysMenu> getMenus(String name,String roleId){
        List<SysMenu> menuList = menuRepositoty.getMenus(name);
        for(SysMenu m:menuList){
            getAllMenusById(m,m.getMenuId()+"" ,roleId);
        }
        return menuList;
    }

    /**
     * 查询子级菜单
     * @author baiyu
     * @date 2018/2/8 11:01
     * @param
     */
    private void getAllMenusById(SysMenu m,String menu_id,String roleId){
        final String sql = "SELECT\n" +
                " DISTINCT	m.*\n" +
                "FROM\n" +
                "	sys_menu m\n" +
                "LEFT JOIN sys_role_menu rm ON m.menu_id = rm.menu_id\n" +
                "LEFT JOIN sys_role rr ON rr.role_id = rm.role_id\n" +
                "WHERE\n" +
                "	m.p_menu_id = '"+menu_id+"'\n" +
                "AND m.sts = 1\n" +
                "AND rr.role_id IN ("+roleId+")" ;
//        final String sql = "SELECT * FROM sys_menu m WHERE m.p_menu_id = ? and sts = 1" ;
        List<SysMenu> list=  queryUtil.query(sql,SysMenu.class);
        m.setSysMenuList(list);
        for(SysMenu mm:m.getSysMenuList()){
            getAllMenusById(mm,mm.getMenuId()+"",roleId);
        }
    }

    /**
     * 查询当前登陆人角色ID
     * @param name
     * @return roleId
     * baiyu
     */
    public String getRoleId(String name){
        String sql ="SELECT\n" +
                "	r.role_id\n" +
                "FROM\n" +
                "	sys_role r\n" +
                "LEFT JOIN sys_emp_role eer ON r.role_id = eer.role_id\n" +
                "LEFT JOIN sys_emp ee ON ee.emp_id = eer.emp_id\n" +
                "WHERE\n" +
                "	ee.login_name = '"+name+"'";
        List<SysRole> list = queryUtil.query(sql,SysRole.class);
        String str ="";
        for(int i=0;i<list.size();i++){
            str += list.get(i).getRoleId()+",";
        }
        return str.substring(0,str.length()-1);
    }
}
